package org.test.client.ui.component.label;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Vector;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.test.client.listener.ClickAction;
import org.test.client.listener.CommonMouseListener;
import org.test.client.listener.ToolTipMouseListener;
import org.test.client.ui.component.window.BlackToolTip;
import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

public class TagLabel extends JComponent {
	
	private TagLabel thisLabel = this;
	private Color tagBorderColor = null;
	private Color tagBackColor1 = null;
	private Color tagBackColor2 = null;
	private Color iconColor = null;
	private int type = 0;
	private String iconStr = null;
	private String[] txts = null;
	private String tagStr = null;
	private int defaultw = 160;
	private int defaulth = 100;
	private int diameter = 50;
	
	public static int minTagw= 18;
	private int tagw = 0;
	private int tagh = 15;
	private int tagDiameter = 16;
	private BufferedImage icon = null;
	private int iconw = 30;
	private List<ClickAction> actions = new Vector<ClickAction>();
	private boolean useToolTip = true;
	private CommonMouseListener mouseListener = null;
	private String toolTipString = "";
	private BlackToolTip toolTip = null;
	
	public TagLabel(int type,String icon,String tagStr,String... txts){
		this.type = type;
		this.iconStr = icon;
		this.tagStr = tagStr;
		this.txts = txts;
		toolTipString = tagStr+" new "+txts[0];
	}
	
	public TagLabel doInit(){
		tagBorderColor = tagBorderColors[type];
		tagBackColor1 = tagColor1s[type];
		tagBackColor2 = tagColor2s[type];
		iconColor = iconColors[type];
		
		icon = Icons.getImgByDesc(iconStr,iconw, iconColor);
		this.setPreferredSize(new Dimension(defaultw,defaulth));
		this.setSize(new Dimension(defaultw,defaulth));
		if(useToolTip){
			mouseListener = new ToolTipMouseListener(this,actions,toolTipString);
		}else{
			mouseListener = new CommonMouseListener(this,actions);
		}
		this.addMouseListener(mouseListener);
		return this;
	}

	
//	@Override
//	public Object doOnEntered(MouseEvent ev) {
//		if(useToolTip){
//			toolTip.showToolTip();
//		}
//		return null;
//	}
//
//	@Override
//	public Object doOnExited(MouseEvent ev) {
//		if(useToolTip){
//			toolTip.hideToolTip();
//		}
//		return null;
//	}
	
	private int marginlr = 3;
	private int margintop = 10;
	private int iconMarginTop = 2;
	private int txtMarginTop = 8;
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		Paint oldPaint = g2d.getPaint();

		int tmpw = this.getWidth();
		int tmph = this.getHeight();
		//绘制主区域
		GradientPaint paint1 = new GradientPaint(0, 0, backColor1, 0, tmph, backColor3);
		BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
		g2d.setStroke(bs1);
		g2d.setColor(labelBorderColor1);//10  3
		int x = marginlr;
		int y = margintop;
		int w = tmpw-2*marginlr;
		int h = tmph-margintop-1;
		g2d.drawRoundRect(x,y,w,h, diameter, diameter);
		
		x = x+2;
		y = y+2;
		w = w-4;
		h = h-4;
		g2d.setPaint(paint1);
		g2d.fillRoundRect(x, y, w, h, diameter, diameter);

		//绘制tag
		g2d.setFont(tagFont);
		FontMetrics fm = g2d.getFontMetrics();
		int tagStrWidth = fm.stringWidth(tagStr);
		int ascent = fm.getAscent();
		int descent = fm.getDescent();
		
		tagw = tagStrWidth+tagDiameter/2;
		if(minTagw > tagw){
			tagw = minTagw;
		}
		g2d.setColor(tagBorderColor);
		x = tmpw-marginlr-tagw;
		y = 1;
		w = tagw;
		h = tagh;
		g2d.drawRoundRect(x,y,w,h,tagDiameter,tagDiameter);
		
		GradientPaint paintTag = new GradientPaint(0, 0, tagBackColor1, tagw, tagh, tagBackColor2);
		g2d.setPaint(paintTag);
		x = x + 1 ;
		y = y + 1;
		w = w - 1;
		h = h - 1;
		g2d.fillRoundRect(x, y, w, h, tagDiameter, tagDiameter);
		
		g2d.setPaint(oldPaint);
		g2d.setColor(tagFontColor);
		int tagFonty = margintop + tagh/2 - (ascent-descent)/2;
		//加1是为了使tag字符串 看起来为中间的位置
		x = tmpw - tagw/2 - tagStrWidth/2-marginlr+1;
		g2d.drawString(tagStr, x, tagFonty);
		
		//绘制文字and Icon
		x = tmpw/2 - iconw/2;
		y = margintop + iconMarginTop;//向下再偏移2px
		g2d.setColor(iconColor);
		g2d.drawImage(icon, x, y, icon.getWidth(), icon.getHeight(), null);
		
		g2d.setFont(font);
		String ptxt = "";
		y =  margintop+icon.getHeight() +iconMarginTop;
		if(txts.length >= 2){
			ptxt = txts[0];
			drawString(g2d,ptxt,y,tmpw,tmph);
			ptxt = txts[1];
			//偏移Y
			y =  y + fm.getAscent() + txtMarginTop;
			drawString(g2d,ptxt,y,tmpw,tmph);
		}else{
			ptxt = txts[0];
			drawString(g2d,ptxt,y,tmpw,tmph);
		}
	}
	
	public void drawString(Graphics2D g2d , String ptxt, int y,int tmpw,int tmph){
		FontMetrics fm = g2d.getFontMetrics();
		int txtw = UIUtil.getTxtWidth(g2d, ptxt);
		int x = tmpw/2 - txtw/2;
		y = y +fm.getAscent() + txtMarginTop;
		if( y < tmph-fm.getDescent()){
			g2d.drawString(ptxt, x, y);
		}
	}
	
	
	public void addClickListener(ClickAction action){
		if(action != null){
			actions.add(action);
		}
	}
	
	public static void main(String[] args) {
		
		JPanel pppp = new JPanel();
		TagLabel tagLabel = new TagLabel(1, "fa-user","8","Total Number","507").doInit();
		pppp.add(tagLabel);

		JButton btn = new JButton("TestToolTip");
		btn.addMouseListener(new ToolTipMouseListener(btn, "这是一个测试"));
		pppp.add(btn);
		

		JLabel label = new JLabel("TestToolTip");
		label.addMouseListener(new ToolTipMouseListener(label, "这是一个测试"));
		pppp.add(label);

//		pppp.setLayout(null);
//		tagLabel.setBounds(10, 10, 100, 80);
		
		JFrame fff = new JFrame();
		Container contentPane = fff.getContentPane();
		contentPane.add(pppp);
//		System.out.println(pppp.getLayout());
		fff.setDefaultCloseOperation(fff.EXIT_ON_CLOSE);
		fff.setSize(400, 300);
		fff.setLocationRelativeTo(null);
		fff.setVisible(true);
	}
	
	public final static int TYPE_BLUE = 0;
	public final static int TYPE_GREEN = 1;
	public final static int TYPE_ORANGE = 2;
	public final static int TYPE_RED = 3;
	
	private Font tagFont = new Font(Font.DIALOG, Font.BOLD, 11);
	private Font font = new Font(Font.DIALOG, Font.BOLD, 13);
	public static Color tagFontColor = Color.WHITE;
	public static Color fontColor = Icons.getColor("4eafe7");
	
	public static Color labelBorderColor1 = Icons.getColor("dedede");
	public static Color labelBorderColor2 = Icons.getColor("fafafa");
	
	public static Color backColor1 = Icons.getColor("d4dfec");
	public static Color backColor2 = Icons.getColor("e8edf5");
	public static Color backColor3 = Icons.getColor("fefeff");
	
	
	public static Color blueTagBorderColor = Icons.getColor("2fabe9");
	public static Color blueTagColor1 = Icons.getColor("92d3f4");
	public static Color blueTagColor2 = Icons.getColor("8cd1f2");
	public static Color blueIconColor = Icons.getColor("3986ac");
	
	public static Color greenTagBorderColor = Icons.getColor("5aad34");
	public static Color greenTagColor1 = Icons.getColor("80d05c");
	public static Color greenTagColor2 = Icons.getColor("91d571");
	public static Color greenIconColor = Icons.getColor("458746");
	
	public static Color orangeTagBorderColor = Icons.getColor("f4a506");
	public static Color orangeTagColor1 = Icons.getColor("fbbd44");
	public static Color orangeTagColor2 = Icons.getColor("fbcd6f");
	public static Color orangeIconColor = Icons.getColor("f99406");

	public static Color redTagBorderColor = Icons.getColor("fa5833");
	public static Color redTagColor1 = Icons.getColor("fb6644");
	public static Color redTagColor2 = Icons.getColor("fb927a");
	public static Color redIconColor = Icons.getColor("b94846");

	public static Color[] tagBorderColors = new Color[]{blueTagBorderColor,greenTagBorderColor,orangeTagBorderColor,redTagBorderColor};
	public static Color[] tagColor1s = new Color[]{blueTagColor1,greenTagColor1,orangeTagColor1,redTagColor1};
	public static Color[] tagColor2s = new Color[]{blueTagColor2,greenTagColor2,orangeTagColor2,redTagColor2};
	public static Color[] iconColors = new Color[]{blueIconColor,greenIconColor,orangeIconColor,redIconColor};

	public TagLabel getThisLabel() {
		return thisLabel;
	}

	public void setThisLabel(TagLabel thisLabel) {
		this.thisLabel = thisLabel;
	}

	public Color getTagBorderColor() {
		return tagBorderColor;
	}

	public void setTagBorderColor(Color tagBorderColor) {
		this.tagBorderColor = tagBorderColor;
	}

	public Color getTagBackColor1() {
		return tagBackColor1;
	}

	public void setTagBackColor1(Color tagBackColor1) {
		this.tagBackColor1 = tagBackColor1;
	}

	public Color getTagBackColor2() {
		return tagBackColor2;
	}

	public void setTagBackColor2(Color tagBackColor2) {
		this.tagBackColor2 = tagBackColor2;
	}

	public Color getIconColor() {
		return iconColor;
	}

	public void setIconColor(Color iconColor) {
		this.iconColor = iconColor;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getIconStr() {
		return iconStr;
	}

	public void setIconStr(String iconStr) {
		this.iconStr = iconStr;
	}

	public String[] getTxts() {
		return txts;
	}

	public void setTxts(String[] txts) {
		this.txts = txts;
	}

	public String getTagStr() {
		return tagStr;
	}

	public void setTagStr(String tagStr) {
		this.tagStr = tagStr;
	}

	public int getDefaultw() {
		return defaultw;
	}

	public void setDefaultw(int defaultw) {
		this.defaultw = defaultw;
	}

	public int getDefaulth() {
		return defaulth;
	}

	public void setDefaulth(int defaulth) {
		this.defaulth = defaulth;
	}

	public int getDiameter() {
		return diameter;
	}

	public void setDiameter(int diameter) {
		this.diameter = diameter;
	}

	public int getTagw() {
		return tagw;
	}

	public void setTagw(int tagw) {
		this.tagw = tagw;
	}

	public int getTagh() {
		return tagh;
	}

	public void setTagh(int tagh) {
		this.tagh = tagh;
	}

	public int getTagDiameter() {
		return tagDiameter;
	}

	public void setTagDiameter(int tagDiameter) {
		this.tagDiameter = tagDiameter;
	}

	public BufferedImage getIcon() {
		return icon;
	}

	public void setIcon(BufferedImage icon) {
		this.icon = icon;
	}

	public int getIconw() {
		return iconw;
	}

	public void setIconw(int iconw) {
		this.iconw = iconw;
	}

	public List<ClickAction> getActions() {
		return actions;
	}

	public void setActions(ArrayList<ClickAction> actions) {
		this.actions = actions;
	}

	public int getMarginlr() {
		return marginlr;
	}

	public void setMarginlr(int marginlr) {
		this.marginlr = marginlr;
	}

	public int getMargintop() {
		return margintop;
	}

	public void setMargintop(int margintop) {
		this.margintop = margintop;
	}

	public int getIconMarginTop() {
		return iconMarginTop;
	}

	public void setIconMarginTop(int iconMarginTop) {
		this.iconMarginTop = iconMarginTop;
	}

	public int getTxtMarginTop() {
		return txtMarginTop;
	}

	public void setTxtMarginTop(int txtMarginTop) {
		this.txtMarginTop = txtMarginTop;
	}

	public Font getTagFont() {
		return tagFont;
	}

	public void setTagFont(Font tagFont) {
		this.tagFont = tagFont;
	}

	public Font getFont() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	public boolean isUseToolTip() {
		return useToolTip;
	}

	public void setUseToolTip(boolean useToolTip) {
		this.useToolTip = useToolTip;
	}

	public String getToolTipString() {
		return toolTipString;
	}

	public void setToolTipString(String toolTipString) {
		this.toolTipString = toolTipString;
	}
	
	

}
